<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
	var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_download.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', 10);
        $this->setState('limitstart', $limitstart);
    }
    
    public function savedownload($data){
    	$sql = "INSERT INTO dowi (DOWI_CODE, DOWI_NAME, DOWI_CADM_CODE,DOWI_PATH, DOWI_USEM_CODE, DOWI_DESCRIPTION, DOWI_DATE) 
                VALUES ($data[DOWI_CODE], '$data[DOWI_NAME]', $data[DOWI_CADM_CODE], '$data[DOWI_PATH]', $data[DOWI_USEM_CODE], '$data[DOWI_DESCRIPTION]', '$data[DOWI_DATE]')";
        
        $this->_db->setQuery($sql);       
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    
    public function remove($cid){
        $cid = implode(',', $cid);
    	$query = "DELETE FROM dowi WHERE DOWI_CODE IN($cid)";
        $this->_db->setQuery($query);
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    //Get DOWI_PATH from id
    public function getDowi_PathFromID($cid){
        $qr = "SELECT DOWI_PATH FROM dowi WHERE DOWI_CODE = $cid";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
       return $result->DOWI_PATH;
    }
    
    //Get all list download collection
	function getAllDownload($list = null){

       $qr = "SELECT DISTINCT DOWI_CODE,DOWI_NAME,CADM_NAME,DOWI_DATE,DOWI_PATH
                     FROM dowi LEFT JOIN cadm ON DOWI_CADM_CODE=CADM_CODE";
        $where = null;
        //search       
       if(!empty($list['filter_search'])){
            $where = " WHERE DOWI_CODE  = '$list[filter_search]' OR DOWI_NAME LIKE '%$list[filter_search]%'";
       }
       
       // get total number record
       $this->_total = $this->getTotal($where);
         
       $qr .= $where;
           
       // order by
       $qr .= " ORDER BY DOWI_CODE DESC"; 
       
       // pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       //echo $qr;
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    function getTotal($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {
            
            $query = "SELECT COUNT(DISTINCT DOWI_CODE)
                     FROM dowi LEFT JOIN cadm ON DOWI_CADM_CODE=CADM_CODE $where";
            
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    //Get list CADM
    function getAllCadm($list = null){

	   $qr = "SELECT CADM_CODE,CADM_NAME FROM cadm";
       // order by
       $qr .= " ORDER BY CADM_CODE ASC"; 
              
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get list CADM for POPUP
    function getAllCadmPopup($list = null){

	   $qr = "SELECT CADM_CODE,CADM_NAME FROM cadm";
       // order by
       $qr .= " ORDER BY CADM_CODE DESC"; 
              
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    //Get DOWI_CODE max
    function getMaxDOWI_CODE($where = null){
        // Load the content if it doesn't already exist
        $query = "SELECT MAX(DOWI_CODE) FROM dowi";
            
        $this->_db->setQuery($query);
        $this->_max = $this->_db->loadResult();
        
        return $this->_max;
    }
    //Get CADM_CODE max
    function getMaxCADM_CODE($where = null){
        // Load the content if it doesn't already exist
        $query = "SELECT MAX(CADM_CODE) FROM cadm";
            
        $this->_db->setQuery($query);
        $this->_max = $this->_db->loadResult();
        
        return $this->_max;
    }
    //save category download
    public function savecadm($data,$addNew){
        if($addNew){
            $sql = "INSERT INTO cadm (CADM_CODE, CADM_NAME) 
                VALUES ($data[CADM_CODE], '$data[CADM_NAME]')";
        }else{
            $sql = "UPDATE cadm 
                    SET CADM_NAME = '$data[CADM_NAME]' WHERE CADM_CODE = $data[CADM_CODE]";
        }
    	
        $this->_db->setQuery($sql);       
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    //get cadm from cadm_code
    public function getCadmFromID($cid){
        $qr = "SELECT CADM_CODE,CADM_NAME FROM cadm WHERE CADM_CODE = $cid";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
       return $result;
    }
    //delete cadm
    public function removeCadm($cid){
        
    	$query = "DELETE FROM cadm WHERE CADM_CODE IN($cid)";
        $this->_db->setQuery($query);
        if($this->_db->query()){
            return true;
        }else{
            return false;
        }
    }
    
}
